package com.wwtech.epp.ui.activity.friend.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.db.Friend;
import com.wwtech.epp.entity.GroupMemberme;
import com.wwtech.epp.entity.NodeMember;
import com.wwtech.epp.entity.multi.PersonItem;
import com.wwtech.epp.ui.activity.friend.AddPersonActivity;
import com.wwtech.epp.ui.activity.friend.contract.AddPersonContract;

import java.util.ArrayList;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

import static com.wwtech.epp.ui.adapter.main.CheckPersonAdapter.TYPE_PERSON;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: presenter of AddPersonActivity
 * @date 2017/09/12 23:05:17
 */
public class AddPersonPresenter implements AddPersonContract.AddPersonContractPresenter {

    HttpAPIWrapper httpAPIWrapper;
    private final AddPersonContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private AddPersonActivity mActivity;
    private ArrayList<Friend> groupMemberList;
    private boolean canDelet = true;

    private String type = "";

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isCanDelet() {
        return canDelet;
    }

    public void setCanDelet(boolean canDelet) {
        this.canDelet = canDelet;
    }

    private ArrayList<Friend> checkedPersonList;

    @Inject
    public AddPersonPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull AddPersonContract.View view, AddPersonActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void init(ArrayList<Friend> list) {
        checkedPersonList = list;
        for (int i = 0; i < ContainValue.userList.size(); i++) {
            if (ContainValue.userList.get(i).getItemType() == TYPE_PERSON) {
                PersonItem temp = (PersonItem) ContainValue.userList.get(i);
                if (!temp.type.equals("contact")) {
                    continue;
                }
                ((PersonItem) ContainValue.userList.get(i)).freind.setChecked(false);
            }
        }
        for (Friend friend : checkedPersonList) {
            for (int i = 0; i < ContainValue.userList.size(); i++) {
                if (ContainValue.userList.get(i).getItemType() == TYPE_PERSON) {
                    PersonItem temp = (PersonItem) ContainValue.userList.get(i);
                    if (!temp.type.equals("contact")) {
                        continue;
                    }
                    if (((PersonItem) ContainValue.userList.get(i)).freind.getUserId().equals(friend.getUserId())) {
                        ((PersonItem) ContainValue.userList.get(i)).freind.setChecked(true);
                    }
                }
            }
        }
        mView.onCheckedPersonChange(ContainValue.userList, list);
    }

    @Override
    public void getGroupMember(String groupId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getGroupMember(groupId, map)
                .subscribe(new Consumer<GroupMemberme>() {
                    @Override
                    public void accept(GroupMemberme groupMemberme) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        handlerGroupPerson(groupMemberme.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    private void handlerGroupPerson(ArrayList<Friend> list) {
        KLog.i("处理群组成员数据");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCanCheck(true);
            for (int j = 0; j < checkedPersonList.size(); j++) {
                if (list.get(i).getUserId().equals(checkedPersonList.get(j).getUserId())) {
                    list.get(i).setChecked(true);
                    break;
                }
            }
        }
        if (!canDelet) {
            if ("发起群聊".equals(type)) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getUserId().equals(ContainValue.userLogin.getData().getUserId() + "")) {
                        KLog.i("设置该用户不能被选中");
                        list.get(i).setCanCheck(false);
                        break;
                    }

                }
            } else {
                for (int i = 0; i < list.size(); i++) {
                    for (int j = 0; j < ContainValue.currentGroupMember.size(); j++) {
                        if (list.get(i).getUserId().equals(ContainValue.currentGroupMember.get(j).getUserId())) {
                            KLog.i("设置该用户不能被选中");
                            list.get(i).setCanCheck(false);
                            break;
                        }
                    }

                }
            }
        }
        groupMemberList = list;
        mView.groupMemberBack(groupMemberList);
    }

    @Override
    public void onCheckChange(Friend friend, boolean isChecked) {
        boolean isInclude = false;
        for (int i = 0; i < groupMemberList.size(); i++) {
            if (groupMemberList.get(i).getUserId().equals(friend.getUserId())) {
                groupMemberList.get(i).setChecked(isChecked);
                break;
            }
        }
        if (isChecked) {
            for (int i = 0; i < checkedPersonList.size(); i++) {
                if (checkedPersonList.get(i).getUserId().equals(friend.getUserId())) {
                    isInclude = true;
                    break;
                }
            }
            if (!isInclude) {
                checkedPersonList.add(friend);
            }
        } else {
            for (int i = 0; i < checkedPersonList.size(); i++) {
                if (checkedPersonList.get(i).getUserId().equals(friend.getUserId())) {
                    checkedPersonList.remove(i);
                    break;
                }
            }
        }
        handlerFriendCheckChange(friend, isChecked);
        mView.onCheckedPersonChange(ContainValue.userList, checkedPersonList);
        mView.onCheckedFriendChange(ContainValue.userList, checkedPersonList);
    }

    @Override
    public void onFriendCheckChange(Friend friend, boolean isChecked) {
        boolean isInclude = false;
        if (isChecked) {
            for (int i = 0; i < checkedPersonList.size(); i++) {
                if (checkedPersonList.get(i).getUserId().equals(friend.getUserId())) {
                    isInclude = true;
                    break;
                }
            }
            if (!isInclude) {
                checkedPersonList.add(friend);
            }
        } else {
            for (int i = 0; i < checkedPersonList.size(); i++) {
                if (checkedPersonList.get(i).getUserId().equals(friend.getUserId())) {
                    checkedPersonList.remove(i);
                    break;
                }
            }
        }
        mView.handlerFriendCheckChange(friend, isChecked);
        mView.onCheckedFriendChange(ContainValue.userList, checkedPersonList);
    }

    private void handlerFriendCheckChange(Friend friend, boolean isChecked) {
        for (int i = 0; i < ContainValue.userList.size(); i++) {
            if (ContainValue.userList.get(i).getItemType() == TYPE_PERSON) {
                PersonItem temp = (PersonItem) ContainValue.userList.get(i);
                if (!temp.type.equals("contact")) {
                    continue;
                }
                if (((PersonItem) ContainValue.userList.get(i)).freind.getUserId().equals(friend.getUserId())) {
                    KLog.i("处理联系人列表的是否选中");
                    ((PersonItem) ContainValue.userList.get(i)).freind.setChecked(isChecked);
                }
            }
        }
    }

    @Override
    public void getNodeMember(String nodeId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getNodeMember(nodeId, map)
                .subscribe(new Consumer<NodeMember>() {
                    @Override
                    public void accept(NodeMember nodeMember) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        handlerGroupPerson(nodeMember.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void onConfirmClick() {
        mView.onConfirmClick(checkedPersonList);
    }
}